import { request } from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
 tabslist:[
   {
     id:"0",
     value:"综合", 
     isActive:true
   },
   {
     id: "1",
     value: "销量",
     isActive: false
   },
   {
     id: "2",
     value: "价格",
     isActive: false
   }
 ],
    goodlist:[]
  },
  querylist:{
   query:'',
   cid:'',
   //当前页码
   pagenum:1,
   //每页总条数
   pagesize:10,
  },
  //总页数
  totlepages:1,
  handletabsindex(e){
console.log(e)
    const { index } = e.detail;
    let {tabslist}=this.data;
    tabslist.forEach((v, i) => { i === index ? v.isActive = true : v.isActive = false})
    this.setData({
      tabslist
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  //  console.log(options)
   this.querylist.cid=options.cid||"",
   this.querylist.query = options.query|| "",
   this.getgoodslist()
  },
  getgoodslist() {
    request({
      url: '/goods/search', 
      data:this.querylist
    }).then(result => {  
    
      let goodlist = result.data.message.goods
      let pages = result.data.message.total
      this.totlepages = Math.ceil(pages / this.querylist.pagesize)
      console.log(this.totlepages)
      // console.log(pages)
      console.log(result.data.message)
      this.setData({
        goodlist: [...this.data.goodlist, ...result.data.message.goods]
      })
      wx.stopPullDownRefresh()
    }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.setData({
        goodlist:[]
      })
      this.querylist.pagenum=1,
      this.getgoodslist()
      
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   console.log("触发了")
    if (this.querylist.pagenum >= this.totlepages){
      console.log("没有下一页了")
      wx:wx.showToast({
        title: '没有下一页了',})
    }else{
      console.log("还有下一页")
      this.querylist.pagenum++
      this.getgoodslist()

    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})